package com.boxuegu.dao;

import com.boxuegu.domain.Fruit;
import com.boxuegu.domain.User;

import java.io.*;
import java.util.ArrayList;

public class UserDao {
    private ArrayList<Fruit> fruits = new ArrayList<>();//创建一个列表存储水果对象
    private ArrayList<User> users = new ArrayList<>();//创建一个列表存储顾客对象
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;

    //将fruit.txt文件中的数据读到fruits列表中并返回一个列表
    public ArrayList<Fruit> findAllFruit() throws IOException {
        try {
            ois = new ObjectInputStream(new FileInputStream("fruit-supermarket-manager\\fruit.txt"));
        } catch (IOException e) {

        }
        Fruit fruit;
        fruits.clear();
        if (ois != null) {
            while (true) {
                try {
                    fruit = (Fruit) ois.readObject();
                    fruits.add(fruit);
                } catch (EOFException | ClassNotFoundException e) {
                    break;
                }
            }
            ois.close();
        }
        return fruits;
    }

    //将fruits列表的数据写到fruit.txt文件中
    public void outputInFruitFile(ArrayList<Fruit> fruits) throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("fruit-supermarket-manager\\fruit.txt"));

        if (fruits != null) {
            for (Fruit fruit : fruits) {
                oos.writeObject(fruit);
            }
        }
        oos.close();
    }

    //将user.txt文件中的数据读到users列表中并返回一个列表
    public ArrayList<User> findAllUser() throws IOException, ClassNotFoundException {
        try {
            ois = new ObjectInputStream(new FileInputStream("fruit-supermarket-manager\\user.txt"));
        } catch (IOException e) {

        }
        User user;
        users.clear();
        if (ois != null) {
            while (true) {
                try {
                    user = (User) ois.readObject();
                    users.add(user);
                } catch (EOFException e) {
                    break;
                }
            }
            ois.close();
        }
        return users;
    }

    //将users列表的数据写到user.txt文件中
    public void outputInUserFile(ArrayList<User> users) throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("fruit-supermarket-manager\\user.txt"));

        if (users != null) {
            for (User user : users) {
                oos.writeObject(user);
            }
        }

        oos.close();
    }

    //接收用户id和最终价格 更改用户余额
    public boolean pay(String id, double price) throws ClassNotFoundException, IOException {
        Boolean flag = false;
        users = findAllUser();
        if (users != null) {
            for (int i = 0; i < users.size(); i++) {
                if (users.get(i) != null && users.get(i).getId().equals(id)) {
                    double surplus = users.get(i).getMoney() - price;
                    users.get(i).setMoney(surplus);
                    outputInUserFile(users);
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    //待修改
    //根据用户id返回用户对象
    public User getUser(String id) throws IOException, ClassNotFoundException {
        User user = null;
        users = findAllUser();
        if (users != null) {
            for (User currentUser : users) {
                if (id.equals(currentUser.getId())) {
                    user = currentUser;
                }
            }
        }
        return user;
    }

}
